package com.joysw.view;

import com.joysw.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.PopupWindow.OnDismissListener;

/**
 * 
* @Title: FoodsPopWindow.java 
* @Package com.joysw.view 
* @Description: 物品页面的pop
* @author LIMINGYAN  
* @date 2015年3月29日 下午2:26:32 
* @version V1.0
 */
public class FoodsPopWindow implements OnDismissListener, OnClickListener {

	private TextView pop_choice_16g, pop_choice_32g, pop_choice_16m,
			pop_choice_32m, pop_add, pop_reduce, pop_num, pop_ok;
	private ImageView pop_del;

	private PopupWindow popupWindow;
	private Context context;
	private OnItemClickListener listener;
	private final int ADDORREDUCE = 1;
	private String str_type = "";

	public FoodsPopWindow(Context context) {
		this.context = context;
		View view = LayoutInflater.from(context).inflate(
				R.layout.adapter_popwindow, null);
		pop_choice_16g = (TextView) view.findViewById(R.id.pop_choice_16g);
		pop_choice_32g = (TextView) view.findViewById(R.id.pop_choice_32g);
		pop_choice_16m = (TextView) view.findViewById(R.id.pop_choice_16m);
		pop_choice_32m = (TextView) view.findViewById(R.id.pop_choice_32m);
		pop_add = (TextView) view.findViewById(R.id.pop_add);
		pop_reduce = (TextView) view.findViewById(R.id.pop_reduce);
		pop_num = (TextView) view.findViewById(R.id.pop_num);
		pop_ok = (TextView) view.findViewById(R.id.pop_ok);
		pop_del = (ImageView) view.findViewById(R.id.pop_del);

		pop_choice_16g.setOnClickListener(this);
		pop_choice_32g.setOnClickListener(this);
		pop_choice_16m.setOnClickListener(this);
		pop_choice_32m.setOnClickListener(this);
		pop_add.setOnClickListener(this);
		pop_reduce.setOnClickListener(this);
		pop_ok.setOnClickListener(this);
		pop_del.setOnClickListener(this);

		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setAnimationStyle(R.style.popWindow_anim_style);
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		popupWindow.setOnDismissListener(this);
	}

	public interface OnItemClickListener {
		public void onClickOKPop();
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.listener = listener;
	}

	public void showAsDropDown(View parent) {
		popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
	}

	public void dissmiss() {
		popupWindow.dismiss();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.pop_choice_16g:

			pop_choice_16g
					.setBackgroundResource(R.drawable.btn_yuanjiao_choice);
			pop_choice_32g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_16m.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32m.setBackgroundResource(R.drawable.btn_yuanjiao);
			str_type = pop_choice_16g.getText().toString();
			break;
		case R.id.pop_choice_32g:
			pop_choice_16g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32g
					.setBackgroundResource(R.drawable.btn_yuanjiao_choice);
			pop_choice_16m.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32m.setBackgroundResource(R.drawable.btn_yuanjiao);

			str_type = pop_choice_32g.getText().toString();
			break;
		case R.id.pop_choice_16m:

			pop_choice_16g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_16m
					.setBackgroundResource(R.drawable.btn_yuanjiao_choice);
			pop_choice_32m.setBackgroundResource(R.drawable.btn_yuanjiao);
			str_type = pop_choice_16m.getText().toString();
			break;
		case R.id.pop_choice_32m:

			pop_choice_16g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32g.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_16m.setBackgroundResource(R.drawable.btn_yuanjiao);
			pop_choice_32m
					.setBackgroundResource(R.drawable.btn_yuanjiao_choice);

			str_type = pop_choice_32m.getText().toString();

			break;
		case R.id.pop_add:
			if (!pop_num.getText().toString().equals("750")) {

				String num_add = Integer.valueOf(pop_num.getText().toString())
						+ ADDORREDUCE + "";
				pop_num.setText(num_add);
			} else {
				Toast.makeText(context, "750", Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.pop_reduce:
			if (!pop_num.getText().toString().equals("1")) {
				String num_reduce = Integer.valueOf(pop_num.getText()
						.toString()) - ADDORREDUCE + "";
				pop_num.setText(num_reduce);
			} else {
				Toast.makeText(context, "<1", Toast.LENGTH_SHORT).show();
			}
			break;

		case R.id.pop_del:
			listener.onClickOKPop();
			dissmiss();

			break;
		default:
			break;
		}
	}

	@Override
	public void onDismiss() {
		// TODO Auto-generated method stub

	}

}
